package onlinejudge.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import onlinejudge.dao.ProblemDao;
import onlinejudge.dao.ProblemLevelDao;
import onlinejudge.dao.ProblemTypeDao;
import onlinejudge.entity.Problem;
import onlinejudge.entity.ProblemLevel;
import onlinejudge.entity.ProblemType;
import onlinejudge.service.ProblemService;
import onlinejudge.service.UserProblemService;
import onlinejudge.vo.ProblemVo;

@Service
public class ProblemServiceImpl implements ProblemService {

	@Resource
	private ProblemDao problemDao;
	@Resource
	private ProblemTypeDao problemTypeDao;
	@Resource
	private ProblemLevelDao problemLevelDao;
	@Resource
	private UserProblemService userProblemService;
	private Map<String, Object> parameters=new HashMap<String, Object>();
	
	@Override
	public List<ProblemVo> queryAllProblem(Map<String, Object> parameters1) {
		List<ProblemVo> problemList=problemDao.queryProblem(parameters1);
		for (ProblemVo problemVo : problemList) {
			problemVo.setSuccessCount(userProblemService.getSuccessCountByProblemId(problemVo.getProblemId()));
		}
		return problemList;
	}

	@Override
	public void addProblem(Problem problem) {
		parameters.clear();
		parameters.put("problemTitle", problem.getProblemTitle());
		parameters.put("problemContent", problem.getProblemContent());
		parameters.put("inputFormat", problem.getInputFormat());
		parameters.put("outputFormat", problem.getOutputFormat());
		parameters.put("typeId", problem.getTypeId());
		parameters.put("levelId", problem.getLevelId());
		parameters.put("limitTime", problem.getLimitTime());
		parameters.put("limitSpace", problem.getLimitSpace());
		parameters.put("problemSource", problem.getProblemSource());
		parameters.put("inputItema", problem.getInputItema());
		parameters.put("inputItemb", problem.getInputItemb());
		parameters.put("inputItemc", problem.getInputItemc());
		parameters.put("outputItema", problem.getOutputItema());
		parameters.put("outputItemb", problem.getOutputItemb());
		parameters.put("outputItemc", problem.getOutputItemc());
		problemDao.addProblem(parameters);
	}

	@Override
	public void updateProblem(Problem problem) {
		parameters.clear();
		parameters.put("problemId", problem.getProblemId());
		parameters.put("problemTitle", problem.getProblemTitle());
		parameters.put("problemContent", problem.getProblemContent());
		parameters.put("inputFormat", problem.getInputFormat());
		parameters.put("outputFormat", problem.getOutputFormat());
		parameters.put("typeId", problem.getTypeId());
		parameters.put("levelId", problem.getLevelId());
		parameters.put("limitTime", problem.getLimitTime());
		parameters.put("limitSpace", problem.getLimitSpace());
		parameters.put("problemSource", problem.getProblemSource());
		parameters.put("inputItema", problem.getInputItema());
		parameters.put("inputItemb", problem.getInputItemb());
		parameters.put("inputItemc", problem.getInputItemc());
		parameters.put("outputItema", problem.getOutputFormat());
		parameters.put("outputItemb", problem.getOutputItemb());
		parameters.put("outputItemc", problem.getOutputItemc());
		problemDao.updateProblem(parameters);
	}

	@Override
	public void deleteProblem(Integer problemId) {
		problemDao.deleteProblem(problemId);
	}

	@Override
	public Integer getProblemCount() {
		parameters.clear();
		return problemDao.queryProblem(parameters).size();
	}

	@Override
	public List<ProblemLevel> queryAllProblemLevel(Map<String, Object> parameters1) {
		return problemLevelDao.queryProblemLevel(parameters1);
	}

	@Override
	public void addProblemLevel(ProblemLevel problemLevel) {
		parameters.clear();
		parameters.put("problemLevelName", problemLevel.getProblemLevelName());
		problemLevelDao.addProblemLevel(parameters);
	}

	@Override
	public void updateProblemLevel(ProblemLevel problemLevel) {
		parameters.clear();
		parameters.put("problemLevelId", problemLevel.getProblemLevelId());
		parameters.put("problemLevelName", problemLevel.getProblemLevelName());
		problemLevelDao.updateProblemLevel(parameters);
	}

	@Override
	public void deleteProblemLevel(Integer problemLevelId) {
		problemLevelDao.deleteProblemLevel(problemLevelId);
	}

	@Override
	public List<ProblemType> queryAllProblemType(Map<String, Object> parameters1) {
		return problemTypeDao.queryProblemType(parameters1);
	}

	@Override
	public void addProblemType(ProblemType problemType) {
		parameters.clear();
		parameters.put("problemTypeName", problemType.getProblemTypeName());
		problemTypeDao.addProblemType(parameters);
	}

	@Override
	public void updateProblemType(ProblemType problemType) {
		parameters.clear();
		parameters.put("problemTypeId", problemType.getProblemTypeId());
		parameters.put("problemTypeName", problemType.getProblemTypeName());
		problemTypeDao.updateProblemType(parameters);
	}

	@Override
	public void deleteProblemType(Integer problemTypeId) {
		problemTypeDao.deleteProblemType(problemTypeId);
	}

	@Override
	public Integer getProblemLevelCount() {
		parameters.clear();
		return problemLevelDao.queryProblemLevel(parameters).size();
	}

	@Override
	public Integer getProblemTypeCount() {
		parameters.clear();
		return problemTypeDao.queryProblemType(parameters).size();
	}

	@Override
	public ProblemVo getProblem(Integer problemId) {
		parameters.clear();
		parameters.put("problemId", problemId);
		return problemDao.queryProblem(parameters).get(0);
	}

}
